package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingProjectPlan;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.utils.ObjectUtil;
import com.zzyl.vo.NursingPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class NursingPlanServiceImpl implements NursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    public static final Long USER_ID = 1671403256519078153L;

    @Override
    public List<NursingPlanVo> findNursingPlanServiceList() {
        List<NursingPlan> nursingPlans = nursingPlanMapper.seleteList();
        List<NursingPlanVo> nursingPlanVoList = BeanUtil.copyToList(nursingPlans, NursingPlanVo.class);
        return nursingPlanVoList;
    }

    @Override
    public PageResponse<NursingPlanVo> pageNursingPlanServiceList(String name, Integer pageNum, Integer pageSize, Integer status) {
        //分页插件
        PageHelper.startPage(pageNum, pageSize);
        //查询数据库  返回数据
        Page<NursingPlan> pg = nursingPlanMapper.pageList(name, status);
        PageResponse<NursingPlanVo> nursingPlanVoPageResponse = PageResponse.of(pg, NursingPlanVo.class);
        return nursingPlanVoPageResponse;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addNursingPlan(NursingPlanDto nursingPlanDto) {
        //转换dto为实体类
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        //统一获取当前时间
        LocalDateTime now = LocalDateTime.now();
        nursingPlan.setCreateTime(now);
        nursingPlan.setUpdateTime(now);
        nursingPlan.setCreateBy(USER_ID);
        nursingPlan.setUpdateBy(USER_ID);
        nursingPlan.setStatus(1);
        //新增护理计划
        nursingPlanMapper.addNursingPlan(nursingPlan);
        Long id = nursingPlan.getId();
        //拿出护理计划关联
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        //添加时间
        if (!ObjectUtil.isEmpty(projectPlans)) {
            projectPlans.forEach(nursingProjectPlan -> {
                nursingProjectPlan.setCreateTime(now);
                nursingProjectPlan.setUpdateTime(now);
                nursingProjectPlan.setPlanId(id);
                nursingProjectPlan.setCreateBy(USER_ID);
                nursingProjectPlan.setUpdateBy(USER_ID);
            });
            //添加关联数据
            nursingProjectPlanMapper.addNursingProjectPlan(projectPlans);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public NursingPlanVo getNursingPlanById(Long id) {
        //根据id查询护理计划
        NursingPlan nursingPlans = nursingPlanMapper.getById(id);
        //转换为vo输出
        NursingPlanVo nursingPlanVo = BeanUtil.toBean(nursingPlans, NursingPlanVo.class);
        //根据id去查询护理计划关联表
        List<NursingProjectPlan> nursingProjectPlans = nursingProjectPlanMapper.selectByIdNursingPlan(id);
        //判断项目是否为空
            //不为空
            nursingPlanVo.setProjectPlans(nursingProjectPlans);
            return nursingPlanVo;

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateNursingPlan(Long id, NursingPlanDto nursingPlanDto) {
        //1.拿出护理项目的集合
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        //2.Dto转换为护理计划实体类
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        //3.根据id来修改计划内容
        nursingPlan.setId(id);
        nursingPlan.setUpdateTime(LocalDateTime.now());
        nursingPlan.setUpdateBy(USER_ID);
        nursingPlanMapper.updateNursingPlan(nursingPlan);
        //4.根据计划id删除关联表的数据
         nursingProjectPlanMapper.deleteById(id);
        //5.重新添加关联数据
        //5.1 添加计划id
        projectPlans.forEach(nursingProjectPlan -> {
            nursingProjectPlan.setPlanId(id);
            nursingProjectPlan.setUpdateTime(LocalDateTime.now());
            nursingProjectPlan.setUpdateBy(USER_ID);
        });
        nursingProjectPlanMapper.addNursingProjectPlan(projectPlans);
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteById(Long id) {
        //根据id删除护理计划
        nursingPlanMapper.deleteById(id);
        //根据id删除护理项目
        nursingProjectPlanMapper.deleteById(id);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        nursingPlanMapper.updateStatus(id,status);
    }
}
